package io.renren.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.bean.ExercisesQuestionInfo;
import io.renren.common.utils.R;
import io.renren.dao.QuestionDao;
import io.renren.entity.QuestionEntity;
import io.renren.entity.StudentEntity;
import io.renren.util.ExercisesUtil;
import io.renren.service.QuestionErrorService;
import io.renren.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("questionService")
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, QuestionEntity> implements QuestionService {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private QuestionErrorService questionErrorService;

    /**
     * 收藏题目
     * 在所有练习中把题目加到错题练习中收藏
     *
     * @param student    学员实体类
     * @param questionId 题目id
     * @return R
     */
    @Override
    public R insertErrorQuestion(StudentEntity student,
                                 Integer questionId) {
        if (questionId == null) {
            return R.fail("参数错误");
        } else {
            QuestionEntity questionEntity = questionService.selectById(questionId);
            if (questionEntity == null) {
                return R.fail("该题目不存在");
            } else {
                // 1添加成功  2已经添加了
                Integer i = questionErrorService.insertErrorQuestion(student.getUserId(), questionId);
                if (i == 1) {
                    return R.ok("收藏成功");
                } else {
                    return R.fail("该题你已经收藏了");
                }
            }


        }
    }

    /**
     * 获取科目总题数
     * @param subjectId 科目id
     * @return
     */
    @Override
    public Integer getSumQuestionNumber(Integer subjectId) {

        Integer sumQuestion = baseMapper.getSumQuestion(subjectId);
        return sumQuestion;
    }

    //查看该学员是否有顺序联系的题目  有就继续联系  没有就创建
    @Override
    public Integer getQuestionRecordNumber(Integer subjectId, Integer studentId) {
        Integer QuestionRecordNumber = baseMapper.getQuestionRecordNumber(subjectId, studentId);
        return QuestionRecordNumber;
    }


    /***
     * 获取随机练习题目
     * @param studentId 学生id
     * @param subjectId 科目id
     * @param sumQuestionNumber 科目总题数
     * @param sort 序号
     * @return
     */
    @Override
    public ExercisesQuestionInfo getRandomQuestion(Long studentId, Integer subjectId, Integer sumQuestionNumber, Integer sort) {
        //获取随机练习题目
        List<Map<String, Object>> questionList = baseMapper.getRandomQuestion(studentId, subjectId, sumQuestionNumber, sort);
        ExercisesQuestionInfo bean = new ExercisesQuestionInfo();
        // 当前科目的(顺序练习、随机练习、错题练习)总题数
        bean.setSumQuestionNumber(sumQuestionNumber);
        // 练习类型，1表示顺序练习；2表示随机练习；3表示错题练习
        bean.setExercisesType(2);
        // 当前已答题数量
        bean.setAnswerNumber(sort - 1);
        return ExercisesUtil.checkQuestion(2, questionList, bean);
    }

    /***
     * 查看试卷题目是否够
     * 0.够，通过
     * 1.单选题不够
     * 2.多选题不够
     * 3.判断题不够
     * @param subjectId 科目id
     * @return
     */
    @Override
    public Integer checkQuestion(Integer subjectId) {
        //获取各题目数量
        Map<String, Object> getQuestion = baseMapper.getQuestion(subjectId);
        if (getQuestion != null) {
            if (getQuestion.get("radioFlag") != null && Double.valueOf(getQuestion.get("radioFlag").toString()).intValue() == 0) {
                return 1;
            }
            if (getQuestion.get("checkFlag") != null && Double.valueOf(getQuestion.get("checkFlag").toString()).intValue() == 0) {
                return 2;
            }
            if (getQuestion.get("judegFlag") != null && Double.valueOf(getQuestion.get("judegFlag").toString()).intValue() == 0) {
                return 3;
            }
        }
        return 0;
    }

}
